package com.connectingworldapp.list.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.connectingworldapp.activity.R;
import com.connectingworldapp.model.Post;
import com.connectingworldapp.util.Library;

public class ListHomeAdapter extends BaseAdapter{
	Context context;
	ArrayList<Post> listPost = new ArrayList<Post>();
	LayoutInflater inflater;
	
	public ListHomeAdapter(Context context, ArrayList<Post> myList){
		super();
		this.context = context;
		this.listPost = myList;
		inflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listPost.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewPost mViewPost;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.layout_list_home, null);
			mViewPost = new ViewPost();
			convertView.setTag(mViewPost);
		}else{
			mViewPost = (ViewPost)convertView.getTag();
		}
		mViewPost.tvName = detail(convertView,R.id.tvName,listPost.get(position).getUserName());
		mViewPost.tvDate = detail(convertView, R.id.tvDate, Library.convertMillisecondsToString(listPost.get(position).getDatetimePost(),"dd/MM/yyyy"));
		mViewPost.tvContent = detail(convertView, R.id.tvContent, listPost.get(position).getContentPost());
		mViewPost.tvLike = detail(convertView, R.id.tvLike, listPost.get(position).getTotalLike()+"");
		mViewPost.tvComment = detail(convertView, R.id.tvComment, listPost.get(position).getTotalComment()+"");
		mViewPost.tvUserComment = detail(convertView, R.id.tvUserComment, listPost.get(position).getContentPost());
		mViewPost.tvContentComment = detail(convertView, R.id.tvContentComment, listPost.get(position).getLastComment());
		return convertView;
	}

	private TextView detail(View v, int resId, String text){
		TextView tv = (TextView)v.findViewById(resId);
		tv.setText(text);
		return tv;
	}
	
	//load Image ?????????
	/*private ImageView detail(View v, int resId, boolean gender){
		ImageView iv = (ImageView)v.findViewById(resId);
		if(gender)
			iv.setImageResource(R.drawable.male);
		else
			iv.setImageResource(R.drawable.female);
		return iv;
	}*/
	
	private class ViewPost{
		TextView tvName, tvDate, tvContent, tvLike, tvComment, tvUserComment, tvContentComment;
		//ImageView iconUser, imgContent, imgComment;
	}
}
